package laps.dataaccess.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import laps.dataaccess.EmployeeDAO;
import laps.model.Employee;


public class EmployeeDAOImpl implements EmployeeDAO {

	private Connection conneciton;

	public EmployeeDAOImpl(Connection conneciton) {
		super();
		this.conneciton = conneciton;
	}
	
	@Override
	public void deleteEmployee(Employee emp)
	{
		String deleteSql="delete from laps_db.employee where name='"+emp.getName().trim()+"';";
		
		Statement st=null;
		
	    try {
			 st= conneciton.createStatement();
			 st.executeUpdate(deleteSql);
			 st.close();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	   
	}


	@Override
	public void insertEmployee(Employee emp) {
		String insertSql="insert into laps_db.employee reports_to, name, password, role, rank,email" +
				          " values(?,?,?,?,?,?)";
		PreparedStatement ps = null;
		
		try {
			ps = conneciton.prepareStatement(insertSql);
			
			ps.setInt(1, emp.getManager_id());
			ps.setString(2, emp.getName());
			ps.setString(3, emp.getPassword());
			ps.setString(4, emp.getRole());
			ps.setString(5, emp.getRank());
			ps.setString(6, emp.getEmail());
		} catch (SQLException e) {
			try {
				ps.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}


	@Override
	public void updateEmployee(Employee emp) {
		PreparedStatement ps = null;
		String updateSql = "update laps_db.employee" + "set name = ?,"
				+ "password = ?," + "role = ?," + "rank = ?,"
				+ "email = ?," + "reports_to = ?" + "where employee_id = ?;";
		
		
		
		try {
			ps = conneciton.prepareStatement(updateSql);
			ps.setString(1, emp.getName());
			ps.setString(2, emp.getPassword());
			ps.setString(3, emp.getRole());
			ps.setString(4, emp.getRank());
			ps.setString(5, emp.getEmail());
			ps.setInt(6, emp.getManager_id());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			try {
				ps.close();
			} catch (SQLException e1) {
				
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
	}


	@Override
	public Employee getEmployee(String employeeID) {
		
		String selectSql="select * from laps_db.employee where employee_id="+ employeeID;
		
		Employee emp = new Employee();
		Statement st = null;
		ResultSet rs= null;
		
		try {
			st= conneciton.createStatement();
			rs = st.executeQuery(selectSql);
			
			while(rs.next()){
				emp.setEmployee_id(rs.getInt("employee_id"));
				emp.setManager_id(rs.getInt("report_to"));
				emp.setName(rs.getString("name"));
				emp.setRank(rs.getString("rank"));
				emp.setRole(rs.getString("role"));
				emp.setEmail(rs.getString("email"));
				
			}
			
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return emp;
	}


	@Override
	public ArrayList<Employee> getSubordinate(int managerID) {
		Statement st = null;
		ResultSet rs= null;
		
		ArrayList<Employee> subordinates = new ArrayList<Employee>();
		String selectSql ="select * from laps_db.employee where reports_to = " + managerID;		
		
		try {
			st= conneciton.createStatement();
			rs = st.executeQuery(selectSql);	
			
			while(rs.next())
			{
				Employee sub = new Employee();
				sub.setName(rs.getString("name"));
				sub.setEmployee_id(rs.getInt("employeeID"));
				sub.setRank(rs.getString("rank"));
				sub.setRole(rs.getString("role"));
				sub.setEmail(rs.getString("email"));
				
				subordinates.add(sub);
				
			}
			
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
			
		
	}
		return subordinates;
	
	
	}
	
		public boolean authenticate(Employee emp) {
			boolean result = false;
			
			
			String sqlStatement = "select * from laps_db.employee where " +
								  "name='" + emp.getName() + "' and " +
								  "password='" +emp.getPassword() + "';";
			
			Statement st = null;
			ResultSet rs = null;
			
			try {
				st= conneciton.createStatement();
				rs =st.executeQuery(sqlStatement);
				if(rs.next()) result=true;
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
}
